//
//  DSZGZXQViewController.m
//  7.13苏宁框架
//
//  Created by app15 on 15/7/23.
//  Copyright (c) 2015年 lovena. All rights reserved.
//

#import "DSZGZXQViewController.h"
#import "DSZGZHeaderView.h"
#import "DSZShowViewController.h"
#import "DSZGZXQTableViewCell.h"
#import "MJExtension.h"
#import "DSZTJGroupModel.h"
#import "DSZTJShowModel.h"
#import "DSZTJCommentModel.h"
#import "UIImageView+WebCache.h"
#import "DSZXQHeaderView.h"
#import "DSZXQView.h"
NSArray *XQShowModelArray;
DSZTJGroupModel *XQGroupModel;
NSArray *XQCommentArray;
@interface DSZGZXQViewController ()<UITableViewDataSource,UITableViewDelegate,CareClickDelegate>
{
    UICollectionView *collection;
    UITableView *table;
}

@end

@implementation DSZGZXQViewController

-(void)viewWillAppear:(BOOL)animated{
    DSZShowViewController *show=(DSZShowViewController*)self.tabBarController;
    [show hiddenShowTabbar:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadNa];
//    self.title=@""
    table=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 529)];
    
    [self.view addSubview:table];
    table.delegate=self;
    table.dataSource=self;
    DSZXQHeaderView *view=[[DSZXQHeaderView alloc]initWithFrame:CGRectMake(0, 0, 320, 130)];
    view.delegate=self;
    //view.backgroundColor=[UIColor clearColor];
    NSInteger i=XQGroupModel.name.length;
    view.titleLabel.frame=CGRectMake(view.titleLabel.frame.origin.x, view.titleLabel.frame.origin.y, i*16, view.titleLabel.frame.size.height);
    view.vipimage.frame=CGRectMake(view.titleLabel.frame.origin.x+i*16+10, view.vipimage.frame.origin.y, 12, 12);

    [view.imageView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",XQGroupModel.image]]];
    view.titleLabel.text=XQGroupModel.name;
    view.intorLabel.text=XQGroupModel.intor;
    view.funLabel.text=[NSString stringWithFormat:@"累计粉丝数 %@",XQGroupModel.funs];
    table.tableHeaderView=view;
    //table.backgroundColor=[UIColor grayColor];
    // Do any additional setup after loading the view.
}


-(void)loadNa{
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 20, 44)];
    UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem];
    button=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 25, 40)];
    [button setImage:[UIImage imageNamed:@"back_black_icon"]  forState:UIControlStateNormal];
    [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:button];
    UIBarButtonItem *barbutton=[[UIBarButtonItem alloc]initWithCustomView:view];
    self.navigationItem.leftBarButtonItem=barbutton;
    
}

-(void)back{
    DSZShowViewController *view=(DSZShowViewController*)self.tabBarController;
    [view hiddenShowTabbar:NO];
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)XqGroupModel:(DSZTJGroupModel*)model{
    XQGroupModel=model;
    NSDictionary *dic=[model keyValues];
    NSArray *array=dic[@"show"];
    NSArray *showModelArray=[DSZTJShowModel modelArrayWithDictArray:array];
    XQShowModelArray=showModelArray;
    NSMutableArray *CommentArray=[[NSMutableArray alloc]init ];
    for (DSZTJShowModel *showmodel in showModelArray) {
        NSDictionary *dic=[showmodel keyValues];
        NSArray *commentarray=dic[@"comment"];
        [CommentArray addObject:commentarray];
    }
    XQCommentArray=CommentArray;
}



#pragma mark  CareClickDelegate代理方法


-(void)clickCareBtn:(UIButton *)btn model:(TopicCellModel *)model{
    
    if (btn.tag == 1) {
        //model.iscare = NO;
        _care--;
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setValue:@(_care) forKey:@"care"];
        btn.layer.borderColor = [UIColor orangeColor].CGColor;
        btn.selected=NO;
        btn.tag = 0;
    }else{
       // model.iscare = YES;
        _care++;
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setValue:@(_care) forKey:@"care"];
        btn.layer.borderColor = [UIColor lightGrayColor].CGColor;
        btn.selected=YES;
        btn.tag = 1;
        
    }
    
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(UIView*)tableView:(UITableView *)table viewForHeaderInSection:(NSInteger)section{
    DSZXQView *view=[[DSZXQView alloc]initWithFrame:CGRectMake(0, 0, 320, 40)];
    //view.backgroundColor=[UIColor redColor];
    return view;
}
-(CGFloat)tableView:(UITableView *)table heightForHeaderInSection:(NSInteger)section{
    return 50;
}
-(NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section{
    return XQShowModelArray.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ID=@"cell";
    DSZTJShowModel *model=XQShowModelArray[indexPath.row];
    DSZGZXQTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:ID];
    if (cell==nil) {
        cell=[[DSZGZXQTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    [cell.showImageView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",model.image]]];
    
    cell.showTitleLabel.text=model.title;
    [cell commentArray:XQCommentArray[indexPath.row]];
    cell.selectionStyle=0;
    return cell;
}
-(CGFloat)tableView:(UITableView *)table heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *array= XQCommentArray[indexPath.row];
    return 580-4*44+44*array.count;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
